﻿<%@ WebHandler Language="C#" Class="detail" %>
using Models;
using System;
using System.Web;
using Newtonsoft.Json;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using System.Collections;

public class detail : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        Uri uri = HttpContext.Current.Request.Url;
        String host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;


        var xxx=HttpContext.Current.Request.Form["paramJson"];
        JObject jo = (JObject)JsonConvert.DeserializeObject(xxx);



        string tid = jo["gid"].ToString();


        if (string.IsNullOrEmpty(tid))
        {
            HttpContext.Current.Response.Write("parameter error");
            return;
        }

        var code = 1;
        var msg = "商品详情";
        var time = DateTime.Now;

        using (Entities bll = new Entities())
        {
            int gid = int.Parse(tid);
            var x = bll.S_Goods.FirstOrDefault(p => p.GID == gid && p.IsLock != true);
            var GetImgs = methods.GetImgs(x.GImg);


            //新建一个JArray对象，可以理解成json对象中 value 位置的数组对象 ->  []


            ArrayList sku_price = new ArrayList();

            var sku = new
            {
                id=gid,
                goods_id=gid,
                price=x.GPrice,
                goods_sku_text="",
                status="up",
                weigh=x.GWeight,
                image=GetImgs[0],
                stock=x.GCounts,
                sales=x.GSales,
                sn="",
                
            };

           sku_price.Add(sku);

            var data = new
            {
                is_sku=0,
                sku_price,
                stock=x.GCounts,   //库存
                id = x.GID,
                title=x.GName,
                content =x.GContent.Replace("src=\"/upload/","src=\""+host+"/upload/"),
                l = x.GName,
                x.SCCLASSID,
                images=GetImgs,
                image =GetImgs[0].Replace("Up","upload"),
                original_price= x.GPriceYJ,
                price = x.GPrice,
                sales= x.GSales,
                x.G_Introduce,
                x.GSort,
                GContent=x.GContent.Replace("src=\"/upload/","src=\""+host+"/upload/"),
                
            };

            context.Response.Write(JsonConvert.SerializeObject(new { code,msg,time,data}));

        }

        HttpContext.Current.Response.End();
        //context.Response.Write("Hello World");


    }





    public bool IsReusable {
        get {
            return false;
        }
    }

}